package written.test;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * @Author: Dang Qi
 * @Date: 2021/4/10  20:05
 * @Description:
 */
public class Jingdong2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        for(int i = 0; i < n; i++){
            nums[i] = sc.nextInt();
        }
        int[] flag = {nums[0], nums[1]};
        dfs(nums, 2, flag, true, 0);
        dfs(nums, 2, flag, false, 0);
        System.out.println(ans);

    }
    static int ans = 0;
    public static void dfs(int[] data, int index, int[] flag, boolean choice, int count){
        if(index >= data.length) {
            ans = Math.max(ans, count);
            return;
        }
        int[] tmp = flag.clone();
        if(choice){
            if(flag[0] == data[index]) count++;
            else flag[0] = data[index];
        }else{
            if(flag[1] == data[index]) count++;
            else flag[1] = data[index];
        }

        dfs(data, index+1, flag, choice, count);
        flag = tmp.clone();
        dfs(data, index + 1, flag, !choice, count);
    }
    static Deque<Integer> left = new LinkedList<>();
    static Deque<Integer> right = new LinkedList<>();

    public static int dfs2(int[] data, int index){
        if(index >= data.length) return 0;
        if(!left.isEmpty()&& left.getFirst()==data[index]){
            return dfs2(data, index+1) + 1;
        }
        if(!right.isEmpty()&& right.getFirst()==data[index]){
            return dfs2(data, index+1) + 1;
        }

        left.addFirst(data[index]);
        int l_n = dfs2(data, index + 1);
        left.removeFirst();
        right.addFirst(data[index]);
        int r_n = dfs2(data, index+1);
        right.removeFirst();
        return l_n > r_n ? l_n : r_n;


    }
}
